//
//  OENormalPayViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/5.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OENormalPayViewController.h"
#define COUNTDOWNDURING (60 * 30)
@interface OENormalPayViewController ()<TMCountDownMangerDelegate>
@property(nonatomic, assign) NSInteger                     count;/**<  */
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */
@property(nonatomic, strong) STLabel                     *duringLable;/**< <##> */
@end

@implementation OENormalPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = OESTRING(@"CONFIRM THE PAMENT");
    self.count = COUNTDOWNDURING;
    [[TMCountDownManger manger] addDelegate:self];
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 40, UIScreenWidth, 30)
                                                     text:OESTRING(@"Remaining payment time")
                                                textColor:SecendTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    
    STLabel * duringLable = [[STLabel alloc] initWithFrame:CGRectMake(0, titleLable.bottom, UIScreenWidth, 40)
                                                     text:[self currentCountDownString]
                                                textColor:FirstTextColor
                                                     font:25
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:duringLable];
    self.duringLable = duringLable;
    
    STLabel * moneyLable = [[STLabel alloc] initWithFrame:CGRectMake(10, duringLable.bottom + 40, UIScreenWidth - 20, 44)
                                                     text:[NSString stringWithFormat:@"VIP MEMBERS TOTAL:$%@",self.money]
                                                textColor:FlatRed
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    [moneyLable st_showTopLine];
    moneyLable.attributedText = [moneyLable.text st_convertAttributeStringWithKeyWord:@"VIP MEMBERS TOTAL:" attributes:@{NSForegroundColorAttributeName:SecendTextColor}];
    [header addSubview:moneyLable];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, moneyLable.bottom, UIScreenWidth, 10)];
    line.backgroundColor = OE_GrayColor;
    [header addSubview:line];
    header.height = line.bottom;
    self.tableView.tableHeaderView = header;
    
    __weak typeof(self) weakSelf =  self;
    STButton * payButton = [[STButton alloc] initWithFrame:CGRectMake(40, 0, UIScreenWidth - 80, 44)
                                                     title:OESTRING(@"PAYMENT")
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:22
                                           backgroundColor:OE_LIMITE_COLOR
                                           backgroundImage:nil
                                                     image:nil];
    payButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    payButton.top = TMUtils.tabBarTop - 40 ;
    [payButton makeImageRight];
    [self.view addSubview:payButton];
    [payButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedPayButton];
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, TMUtils.navgationBarBootom, UIScreenWidth, payButton.top - TMUtils.navgationBarBootom);
    
    
    
    [OEUserDataController sendGetPayMethod:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            NSArray * array = (id)resp;
            NSMutableArray * dataSouce = NSMutableArray.new;
            for (NSDictionary * dic in array) {
                NSString * pay_name = [dic[@"pay_name"] description];
                NSString * pay_id  = [dic[@"pay_id"] description];
                //                NSString * pay_code;
                BOOL selected = NO;
                if ([array indexOfObject:dic] == 0) {
                    selected = YES;
                }
                
                STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:pay_name accessoryView:[self buttonWithSelcted:selected]];
                model.isSlected = selected;
                [dataSouce addObject:model];
                model.obj_id = pay_id;
            }
            self.dataSouce = dataSouce.copy;
            [self.tableView reloadData];
        }
    }];
    
    
}
- (STButton*)buttonWithSelcted:(BOOL)selected{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"36×36未选中"]];
    buyButton.userInteractionEnabled = NO;
    [buyButton setImage:[UIImage imageNamed:@"34×34选中"] forState:UIControlStateSelected];
    buyButton.selected = selected;
    return buyButton;
}
- (NSString*)currentCountDownString{
  return   [NSDate st_countDownWithSecend:self.count];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    for (STTableEasyModel * emodel in self.dataSouce) {
        UIButton * button = (id)emodel.accessoryView;
        button.selected = NO;
        emodel.isSlected = NO;
    }
    UIButton * button = (id)model.accessoryView;
    button.selected = YES;
    model.isSlected = YES;
    [self.tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --TMCountDownMangerDelegate
- (void)countDownManger:(TMCountDownManger *)countDownManger everySecendRun:(NSInteger)runSectends{
    self.count -- ;
    if (self.count > -1) {
       self.duringLable.text = [self currentCountDownString];
    }else{
        [self st_showAlertTitle:@"在倒计时之内没有完成支付，系统自动取消" message:@"" andWithBlock:^(NSString *name) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    
    
}
#pragma mark --Action Method
- (void)onSelctedPayButton{
    NSString * money = self.money;
    if (!money.length) {
        [SVProgressHUD showInfoWithStatus:OESTRING(@"Please input or chose money")];
        return;
    }
    BOOL isOpenFaceOrFinger = [[NSUserDefaults standardUserDefaults] boolForKey:OE_FACE_OR_FINGER_PAY_OPEN_KEY];
    if (isOpenFaceOrFinger) {
        [TMUtils showFaceOrFingerPayType:@"2" handle:^{
            [self sendPayRequest];
        } inputPwdHandle:^{
            OEPwdInPutViewController * vc = OEPwdInPutViewController.new;
            vc.pwd = STUserManger.defult.loginedUser.pay_password;
            [vc setOnInputTruePwd:^{
                [self sendPayRequest];
            }];
            [self presentViewController:vc animated:NO completion:nil];
            
        } errorHandle:^{
            
        }];
    }else{
        OEPwdInPutViewController * vc = OEPwdInPutViewController.new;
        vc.pwd = STUserManger.defult.loginedUser.pay_password;
        [vc setOnInputTruePwd:^{
            [self sendPayRequest];
        }];
        [self presentViewController:vc animated:NO completion:nil];
    }

}
- (void)sendPayRequest{
    NSString * money = self.money;
    NSString * pay_id;
    for (STTableEasyModel * model in self.dataSouce) {
        if (model.isSlected) {
            pay_id = model.obj_id;
        }
    }
    
    [OEUserDataController sendPayRequestWithMoney:money pay_id:pay_id handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:OESTRING(@"Pay Success")];
            OEPaySuccessViewController * vc =  OEPaySuccessViewController.new;
            vc.recharge_id = [resp[@"recharge_id"] description];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
    

}
@end
